<?php
/**
 * Created by PhpStorm.
 * User: lizi
 * Date: 2021/09/03
 * Time: 17:53
 */

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\ArticleType;
use App\Models\Help;
use App\Models\WebCount;
use App\Services\CommonService;
use Illuminate\Http\Request;

//关于我们、帮助中心
class HelpController
{
    //获取关于我们分类 http://shop.cn/Alipay/Help/getAboutUsType?type_name=关于我们
    public function getAboutUsType(Request $request)
    {
        $input = $request->input();
        if (empty($input['type_name'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $first = ArticleType::GetOneArticleType(array('type_name' => $input['type_name'], 'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY, 'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        if (empty($first)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '信息不存在');
        }
        $second = ArticleType::GetSecondCategory(array('parentid' => $first['id'], 'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY, 'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $second);
    }

    //获取关于我们 http://shop.cn/Alipay/Help/getAboutUs?type_id=
    public function getAboutUs(Request $request)
    {
        $input = $request->input();
        if (empty($input['type_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $data = Article::select('id', 'type_id', 'parentid', 'article_title', 'content')
            ->where(array('type_id' => $input['type_id'], 'xcx_show' => Article::ARTICLE_XCX_IS_SHOW, 'status' => Article::ARTICLE_STATUS_IS_RELEASE, 'is_delete' => Article::ARTICLE_NOT_DELETE))
            ->first();
        $data = $data ? $data->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取常见问题分类 http://shop.cn/Alipay/Help/getCommonProblemType?type_name=常见问题
    public function getCommonProblemType(Request $request)
    {
        $input = $request->input();
        
        //全部
        $type = $input['type'] ? $input['type']:'';
        $where = [];
        if($type){
            $where[] = ['type_id',$type]; 
        }
        $data = Article::select('id', 'type_id', 'parentid', 'article_title')
            ->where(array('xcx_show' => Article::ARTICLE_XCX_IS_SHOW, 'status' => Article::ARTICLE_STATUS_IS_RELEASE, 'is_delete' => Article::ARTICLE_NOT_DELETE))
            ->where($where)
            ->limit(8)
            ->orderBy(Article::raw('RAND()'))
            ->take(8)
            ->get();
        $data = $data ? $data->toArray() : [];
        // dd($data);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取常见问题 http://shop.cn/Alipay/Help/getCommonProblemById?id=
    public function getCommonProblemById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $data = Article::select('id', 'type_id', 'parentid', 'article_title', 'content')
            ->where(array('id' => $input['id'], 'xcx_show' => Article::ARTICLE_XCX_IS_SHOW, 'status' => Article::ARTICLE_STATUS_IS_RELEASE, 'is_delete' => Article::ARTICLE_NOT_DELETE))
            ->first();
        $data = $data ? $data->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 获取服务协议
     */
    public function getAgreement(){
        $agreement = Help::where('id',25)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }

    /**
     * 获取服务协议
     */
    public function getPrivacy(){
        $agreement = Help::where('id',26)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }
    
    
    /**
     * 获取个人征信授权书
     */
    public function getPersonAgreement(){
        $agreement = Help::where('id',27)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }
    
    /**
     * 获取融资租赁委托担保合同
     */
    public function getFinancing(){
        $agreement = Help::where('id',28)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }
    
    /**
     * 获取用户授权协议
     */
    public function getUserAgreement(){
        $agreement = Help::where('id',29)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }
    
    /**
     * 获取数字证书协议
     */
    public function getCert(){
        $agreement = Help::where('id',30)->select('name','content')->first();
        $agreement = obToArray($agreement);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $agreement);
    }
    

    //统计落地页访问人数--抖音
    public function getWebCounts1(Request $request)
    {
        $input = $request->input();
        
        $web_count = WebCount::where('year',date('Y'))
                        ->where('month',date('m'))
                        ->where('day',date('d'))
                        ->where('order_from','抖音')
                        ->select('id')
                        ->first();
        $web_count = $web_count ? $web_count->toArray() : [];
        if(!empty($web_count)){
            //有则更新人数
            WebCount::where('year',date('Y'))
                        ->where('month',date('m'))
                        ->where('day',date('d'))
                        ->where('order_from','抖音')
                        ->select('number')
                        ->increment('number');
        }else{ 
            //无则添加数据
            WebCount::addLog('抖音');
        }
        
    }

}
